package com.ymttest.database.sqlwapper.hunter;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.exceptions.PersistenceException;

import com.ymt.database.SqlManager;
import com.ymttest.database.model.hunter.HunterRelation;

public class HunterRelationWrapper {
	
	SqlManager manager;
	int insertNum = 0;

    public HunterRelationWrapper() {
        manager = new SqlManager("hunter");
    }
    
    @SuppressWarnings("unchecked")
	public HunterRelation selectByPrimaryKey(Long hunterId) {
    	
    	HunterRelation hunterRelation = new HunterRelation();
    	List<HunterRelation> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.hunter.HunterRelationMapper.selectByPrimaryKey",
                        hunterId);
        manager.commit();
        
		if (result.size() > 0) {
			hunterRelation = result.get(0);
		} else
			return null;
		
        return hunterRelation;
    }
    
    // 获取某个推手的所有下级
 	@SuppressWarnings("unchecked")
	public List<HunterRelation> selectSubordinateByHunterId(Long parentHunterId) {

 		List<HunterRelation> result = manager
 				.getSelectResults(
 						"com.ymttest.database.mapping.hunter.HunterRelationMapper.selectSubordinateByHunterId",
 						parentHunterId);
 		manager.commit();

 		if (result.size() == 0) {
 			return null;
 		}

 		return result;
 	}
 	
 	// 获取某个推手的所有下级
  	@SuppressWarnings("unchecked")
  	public List<HunterRelation> selectSubordinateByHunterIdWithLimit(Long parentHunterId) {

  		List<HunterRelation> result = manager
  				.getSelectResults(
  						"com.ymttest.database.mapping.hunter.HunterRelationMapper.selectSubordinateByHunterIdWithLimit",
  						parentHunterId);
  		manager.commit();

  		if (result.size() == 0) {
  			return null;
  		}

  		return result;
  	}
  	
  	// 通过code获取某个推手
   	@SuppressWarnings("unchecked")
   	public HunterRelation selectHunterInfoByCode(String code) {

   		List<HunterRelation> result = manager
   				.getSelectResults(
   						"com.ymttest.database.mapping.hunter.HunterRelationMapper.selectHunterInfoByCode",
   						code);
   		manager.commit();

   		if (result.size() == 0) {
   			return null;
   		}

   		return result.get(0);
   	}
 	
	// 通过下级名称和hunterId获取下级
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public HunterRelation selectSubordinateByHunterIdAndSubordinateName(
			Long hunterId, String subordinateName) {

		HunterRelation hunterRelation = new HunterRelation();

		Map m = new HashMap<String, String>();

		m.put("parent_hunter_id", hunterId);
		m.put("hunter_name", subordinateName);

		List<HunterRelation> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.hunter.HunterRelationMapper.selectSubordinateByHunterIdAndSubordinateName",
						m);
		manager.commit();

		if (result.size() > 0) {
			hunterRelation = result.get(0);
		}

		return hunterRelation;
	}
	
	// 通过下级id和hunterId获取下级
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public HunterRelation selectSubordinateByHunterIdAndSubordinateId(
			Long hunterId, Long subordinateUserId) {

		HunterRelation hunterRelation = new HunterRelation();

		Map m = new HashMap<String, String>();

		m.put("parent_hunter_id", hunterId);
		m.put("hunter_id", subordinateUserId);

		List<HunterRelation> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.hunter.HunterRelationMapper.selectSubordinateByHunterIdAndSubordinateId",
						m);
		manager.commit();

		if (result.size() > 0) {
			hunterRelation = result.get(0);
		}

		return hunterRelation;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<HunterRelation> selectHunterContributionList(Long hunterId) {

		Map m = new HashMap<String, String>();

		m.put("hunterId", hunterId);

		List<HunterRelation> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.hunter.HunterRelationMapper.selectHunterContributionList",
						m);
		manager.commit();

		return result;
	}
 	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public HunterRelation updateLevelByHunterId(
			Long hunterId, int level) {

		HunterRelation hunterRelation = new HunterRelation();

		Map m = new HashMap<String, Object>();

		m.put("hunter_id", hunterId);
		m.put("hunter_level", level);

		List<HunterRelation> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.hunter.HunterRelationMapper.updateLevelByHunterId",
						m);
		manager.commit();

		if (result.size() > 0) {
			hunterRelation = result.get(0);
		}

		return hunterRelation;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public HunterRelation updateStatusByHunterId(Long hunterId, int status) {

		HunterRelation hunterRelation = new HunterRelation();

		Map m = new HashMap<String, Object>();

		m.put("hunter_id", hunterId);
		m.put("status", status);

		List<HunterRelation> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.hunter.HunterRelationMapper.updateStatusByHunterId",
						m);
		manager.commit();

		if (result.size() > 0) {
			hunterRelation = result.get(0);
		}

		return hunterRelation;
	}
   
    public int insert(HunterRelation hunterRelation) {
		
    	int count = 0;
    	
		try {
			count = manager
					.insert("com.ymttest.database.mapping.hunter.HunterRelationMapper.insert",
							hunterRelation);
			manager.commit();

		} catch (PersistenceException e) {
			// 如果插入了重复的记录，则删除原来的记录再插入一次
			e.printStackTrace();
			
//			int deleteCount = deleteByInviteCode(hunterRelation.getCode());
//			
//			if (deleteCount == 1) {
//				
//				insertNum = insertNum + 1;
//				
//				if (insertNum < 3) 
//					insert(hunterRelation);
//			}
		}
    	
		return count;
    }
    
    public int deleteByInviteCode(String inviteCode) {
    	
    	int count = manager.delete("com.ymttest.database.mapping.hunter.HunterRelationMapper.deleteByInviteCode", inviteCode);
    	manager.commit();
    	
    	return count;
    }
    
    public static void main(String[] args) throws ParseException {
    	HunterRelationWrapper test = new HunterRelationWrapper();
    	
    	HunterRelation relation = new HunterRelation();
    	relation.setHunter_id(10002L);
    	relation.setHunter_name("mybatis");
    	relation.setHunter_level(0);
    	relation.setParent_hunter_id(0L);
    	relation.setStatus(0);
    	relation.setAdd_time(new Date());
    	relation.setUpdate_time(new Date());
    	
    	System.out.println(test.insert(relation));
    	
		
//        System.out.println(test.updateLevelByHunterId(25114532L, 2));
        // test.insertYmtCouponSetting(3,11);
        // Logger.debug(test.getLastYmtCouponSettingId());
    }
}
